/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cratos.xxgram.platf.base;

import com.cratos.xxgram.platf.user.UserService;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import javax.persistence.*;
import org.redkale.convert.*;
import org.redkale.source.VirtualEntity;
import org.redkale.util.Comment;
import org.redkale.util.Reproduce;

/**
 *
 * @author zhangjx
 */
/**
 * 头像的url： http://redkale.org/dir/face_xx/{userid的36进制}.jpg
 *
 * @author zhangjx
 */
@VirtualEntity(direct = true, loader = UserInfoLoader.class)
public class UserInfo extends BaseEntity {

    private static final Reproduce<UserInfo, UserInfo> reproduce = Reproduce.create(UserInfo.class, UserInfo.class);

    @Comment("用户名词 长度最大值")
    public static final short USERNAME_MAXLEN = 20;
    
    //男
    public static final short GENDER_MALE = 2;

    //女
    public static final short GENDER_FEMALE = 4;

    //平台的虚拟用户ID
    public static final int USERID_SYSTEM = 2_0000_0000;

    public static final UserInfo USER_SYSTEM = new UserInfo();

    public static final UserInfo USER_MSG_SYSTEM = new UserInfo(1);
   
    // ecdh 序号 默认从100开始
    public static final short ECDH_DEFAULT_SEQNUM = 100;

    // 不启用 销毁密码
    public static final short DISABLE_DESTROY_ACCOUNT = 10;

    // 启用   启用销毁
    public static final short ENABLE_DESTROY_ACCOUNT = 20;

    @Comment("离线")
    public static final short ONLINE_STATUS_OFFLINE = 10;

    @Comment("在线")
    public static final short ONLINE_STATUS_ONLINE = 20;
    
    // 默认密码不可见字符
    public static final byte[] DEFAULT_PWD = new byte[]{0x0A}; // 默认密码不可见字符

    static {
        USER_SYSTEM.setUserid(USERID_SYSTEM);
        USER_SYSTEM.setUsername("SYSTEM");
        USER_SYSTEM.setMobile("");
    }

    @Id
    @Column(comment = "[用户ID] 值从2_0000_0001开始; 36进制固定长度为6位")
    protected int userid;  //用户ID

    @Column(length = 128, comment = "[用户昵称]")
    protected String username = "";  //用户昵称

    @Column(comment = "[用户类型]")
    protected short type;    //用户类型 （前端不可见）

    @Column(length = 128, comment = "密码")
    protected String password = ""; //密码（前端不可见） 数据库存放的密码规则为: HEX-SHA1( HEX-MD5( HEX-MD5(明文)+"-REDKALE" ) +"-REDKALE" )

    @Column(length = 128, comment = "[用户账号]")
    protected String account = "";  //用户账号（前端不可见）

    @Column(comment = "电话号码国级 默认中国")
    protected int nation = 86;

    @Column(length = 128, comment = "[手机号码]")
    protected String mobile = "";  //手机号码（前端不可见）

    @Column(length = 1024, comment = "头像")
    protected String face = "";

    @Column(length = 255, comment = "融云token")
    protected String rytoken = "";  //融云token （前端不可见）

    @Column(comment = "融云token更新时间")
    protected long rytime;

    @Column(comment = "个人信息更新时间")
    protected long infotime;

    @Column(length = 16, comment = "APP的设备系统(小写); android/ios/web/wap")
    protected String appos = "";//APP的设备系统 （前端不可见） 

    @Column(length = 255, comment = "APP的设备ID")
    protected String apptoken = "";  //APP的设备ID （前端不可见） 通常用于IOS的APNS推送

    @Column(comment = "[状态]: 10:正常;20:待审批;30:审批不通过;40:冻结;50:隐藏;60:关闭;70:过期;80:删除;")
    protected short status;    //状态 （前端不可见）  值见BaseEntity的STATUS常量

    @Transient
    @Comment("[用户在线状态]: 10:离线;20:在线;")
    protected short onlinestatus = ONLINE_STATUS_OFFLINE;    //状态 （前端不可见）  值见BaseEntity的STATUS常量
    
    @Column(comment = "[性别]：2：男； 4:女；")
    protected short gender; //性别; 2:男;  4:女; 值见BaseEntity的GENDER常量

    @Column(comment = "ecdh加密私钥")
    protected String ecdhprivkey = "";

    @Column(comment = "ecdh加密公钥")
    protected String ecdhpubkey = "";

    @Column(comment = "上一次ecdh加密私钥")
    protected String preecdhprivkey = "";

    @Column(comment = "上一次ecdh加密公钥")
    protected String preecdhpubkey = "";

    @Column(comment = "ecdhseqnum 密钥生成序号 从100 开始")
    protected int ecdhseqnum = ECDH_DEFAULT_SEQNUM;

    @Column(comment = "yyyy-mm-dd 出生 年月日")
    protected String birthday = "";

    @Column(comment = "个性签名 255字符")
    protected String personlsign = "";

    @Column(comment = "用户注销时间")
    protected long logouttime;

    @Comment("记录登录时间")
    @Transient
    protected long logintime;
    
    @Column(comment = "第1位表示 消息通知状态 "
            + " 第2位表示 语音通话提醒状态   "
            + "第3位表示 应用打开时声音状态 "
            + " 第4位表示 打开时震动  "
            + "第5位表示 启用id解锁 "
            + "第6位表示 启用指纹解锁"
            + "  第7位表示 销毁密码设置  "
            + "  第8位表示 清理聊天记录"
            + "  第9位表示 清理群聊记录  "
            + "第10位表示 清理前通知我"
            // 
            + "第11位表示 为我朋友时需要验证"
            + "第12位表示 输入状态显示"
            + "第13位表示 阅读状态显示"
            // 添加方式
            + "第14位表示 添加我的方式  手机号码"
            + "第15位表示 添加我的方式  ID添加"
            + "第16位表示 添加我的方式  我的二维码")
    protected long syssetting = 0xffff;//默认全部开启

    public static final int S_1_NOTIFY_STATUS = 0x1;
    public static final int S_2_VOICE_STATUS = 0x1;
    public static final int S_3_OPEN_VOICE_STATUS = 0x1;
    public static final int S_4 = 0x1;
    public static final int S_5 = 0x1;
    public static final int S_6 = 0x1;
    public static final int S_7 = 0x1;
    public static final int S_8 = 0x1;
    public static final int S_9 = 0x1;
    public static final int S_10 = 0x1;
    public static final int S_12 = 0x1;
    
    @Column(comment = "解锁密码值")
    public String lockpwd = "";

    @Column(comment = "销毁密码")
    protected String destroypwd = "";

    @Column(comment = "清理周期 10 天 20 周 30 月 40 自定义")
    protected short clearcycle = 0;

    @Column(comment =" 极光推送app 注册id")
    protected String jpregid;
    
    @Column(comment ="用户周期 1 新用户; 2 老用户")
    protected short usercycle = 1;
    
    @Comment("新用户")
    public static short USER_CYCLE_NEW = 1;
    
    @Comment("老用户")
    public static short USER_CYCLE_OLD = 2;
    
    @Transient
    protected int haspwd;

    @Comment("添加jsession 便于websocket 在登录后返回 sessionid 避免每次根据UNF 都登录")
    @Transient
    protected String jsessionid;

    @Column(comment ="终端在线类型 第一位  1 App 第二位   web")
    protected int onlineterminal = 0;
    
    // web 在线
    public boolean isWebOnline() {
    	return (onlineterminal & 2) == 2;
    }
    
    // App 在线
    public boolean isAppOnline() {
    	return (onlineterminal & 1) == 1;
    }
    
    
  
    public int getOnlineterminal() {
		return onlineterminal;
	}

	public void setOnlineterminal(int onlineterminal) {
		this.onlineterminal = onlineterminal;
	}

	public long getLogintime() {
		return logintime;
	}

	public void setLogintime(long logintime) {
		this.logintime = logintime;
	}

	public UserInfo() {
    	
    }
    
    public short getUsercycle() {
		return usercycle;
	}

	public void setUsercycle(short usercycle) {
		this.usercycle = usercycle;
	}

	public UserInfo(int userid) {
    	this.userid = userid;
    }
    public String getJpregid() {
        return jpregid;
    }

    public void setJpregid(String jpregid) {
        this.jpregid = jpregid;
    }

    public short getOnlinestatus() {
		return onlinestatus;
	}

	public void setOnlinestatus(short onlinestatus) {
		this.onlinestatus = onlinestatus;
	}

	public String getJsessionid() {
        return jsessionid;
    }

    public void setJsessionid(String jsessionid) {
        this.jsessionid = jsessionid;
    }
    
    // ("默认没密码 0 没设置密码  1 已设置密码")
    public int getHaspwd() {
        // 三次密码加密 
        if (this.password == null
                || this.password.isEmpty()
                || UserService.digestPassword(UserService.secondPasswordMD5(md5(UserInfo.DEFAULT_PWD))).equals(this.getPassword())) {
            return 0;
        }
        return 1;
    }

    public static String md5(byte[] data) {
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("md5");
            messageDigest.update(data);
            java.math.BigInteger d = new java.math.BigInteger(1, messageDigest.digest());
            return d.toString(16);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void setHaspwd(int haspwd) {
        this.haspwd = haspwd;
    }

    public short getClearcycle() {
        return clearcycle;
    }

    public void setClearcycle(short clearcycle) {
        this.clearcycle = clearcycle;
    }

    public String getDestroypwd() {
        return destroypwd;
    }

    public void setDestroypwd(String destroypwd) {
        this.destroypwd = destroypwd;
    }

    public String getLockpwd() {
        return lockpwd;
    }

    public void setLockpwd(String lockpwd) {
        this.lockpwd = lockpwd;
    }

    public long getSyssetting() {
        return syssetting;
    }

    public void setSyssetting(long syssetting) {
        this.syssetting = syssetting;
    }

//    @ConvertColumn(ignore = true, type = ConvertType.JSON)
    public String getPreecdhprivkey() {
        return preecdhprivkey;
    }

    public void setPreecdhprivkey(String preecdhprivkey) {
        this.preecdhprivkey = preecdhprivkey;
    }

    public long getLogouttime() {
        return logouttime;
    }

    public void setLogouttime(long logouttime) {
        this.logouttime = logouttime;
    }

    public String getPreecdhpubkey() {
        return preecdhpubkey;
    }

    public void setPreecdhpubkey(String preecdhpubkey) {
        this.preecdhpubkey = preecdhpubkey;
    }

    public String getBirthday() {
        return birthday;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

    public String getPersonlsign() {
        return personlsign;
    }

    public void setPersonlsign(String personlsign) {
        this.personlsign = personlsign;
    }

    public int getEcdhseqnum() {
        return ecdhseqnum;
    }

    public void setEcdhseqnum(int ecdhseqnum) {
        this.ecdhseqnum = ecdhseqnum;
    }

//    @ConvertColumn(ignore = true, type = ConvertType.JSON)
    public String getEcdhprivkey() {
        return ecdhprivkey;
    }

    public void setEcdhprivkey(String ecdhprivkey) {
        this.ecdhprivkey = ecdhprivkey;
    }

    public String getEcdhpubkey() {
        return ecdhpubkey;
    }

    public void setEcdhpubkey(String ecdhpubkey) {
        this.ecdhpubkey = ecdhpubkey;
    }

    public UserInfo copy() {
        return reproduce.apply(new UserInfo(), this);
    }

    public UserInfo copyTo(UserInfo dest) {
        return reproduce.apply(dest, this);
    }

    public boolean checkAuth(int moduleid, int actionid) {
        if (moduleid == 0 || actionid == 0) {
            return true;
        }
        //权限判断
        return true;
    }

    public String getFace() {
        return face;
    }

    public void setFace(String face) {
        this.face = face;
    }

    public String getAppos() {
        return appos;
    }

    public void setAppos(String appos) {
        this.appos = appos;
    }

    public int getUserid() {
        return userid;
    }

    public String getUser36id() {
        return Integer.toString(userid, 36);
    }

    public void setUserid(int userid) {
        this.userid = userid;
    }

    public int getNation() {
        return nation;
    }

    public void setNation(int nation) {
        this.nation = nation;
    }

    //用户帐号不允许输出给外部接口
    @ConvertColumn(ignore = true, type = ConvertType.JSON)
    public String getAccount() {
        return account == null ? "" : account;
    }

    public void setAccount(String account) {
        this.account = account == null ? "" : account.trim();
    }

    public short getType() {
        return type;
    }

    public void setType(short type) {
        this.type = type;
    }

    public String getUsername() {
        return username == null ? "" : username;
    }

    public void setUsername(String username) {
        this.username = username == null ? "" : username.trim();
    }

    public long getInfotime() {
        return infotime;
    }

    public void setInfotime(long infotime) {
        this.infotime = infotime;
    }

    //密码不允许输出给外部接口
    @ConvertColumn(ignore = true, type = ConvertType.JSON)
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password == null ? "" : password.trim();
    }

    //手机号码不允许输出给外部接口
    //    @ConvertColumn(ignore = true, type = ConvertType.JSON)
    public String getMobile() {
        return mobile == null ? "" : mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile == null ? "" : mobile.trim();
    }

    //融云Token不允许输出给外部接口
//    @ConvertColumn(ignore = true, type = ConvertType.JSON)
    public String getRytoken() {
        return rytoken == null ? "" : rytoken;
    }

    public void setRytoken(String rytoken) {
        this.rytoken = rytoken == null ? "" : rytoken.trim();
    }

    @ConvertColumn(ignore = true, type = ConvertType.JSON)
    public long getRytime() {
        return rytime;
    }

    public void setRytime(long rytime) {
        this.rytime = rytime;
    }

    //APP设备ID不允许输出给外部接口
    @ConvertColumn(ignore = true, type = ConvertType.JSON)
    public String getApptoken() {
        return apptoken == null ? "" : apptoken;
    }

    public void setApptoken(String apptoken) {
        this.apptoken = apptoken == null ? "" : apptoken.trim();
    }

    //用户状态值不允许输出给外部接口
    @ConvertColumn(ignore = true, type = ConvertType.JSON)
    public short getStatus() {
        return status;
    }

    public void setStatus(short status) {
        this.status = status;
    }

    public short getGender() {
        return gender;
    }

    public void setGender(short gender) {
        this.gender = gender;
    }

    public static void main(String[] args) {
        System.out.println("" + UserService.digestPassword("50d43dfa3df9c98e2fdbc1320afc4119"));;
    }
}
